#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
struct ListNode {
    int val;
    struct ListNode* next;
};
typedef struct ListNode ListNode;
struct ListNode* detectCycle(struct ListNode* head) {
    if (head == NULL || head->next == NULL) {
        return NULL;
    }
    ListNode* fast = head, * slow = head;
    ListNode* left = head, * right;
    while (fast && fast->next) {
        fast = fast->next->next;
        slow = slow->next;
        if (fast == slow)
        {
            right = slow;
            while (left != right) {
                left = left->next;
                right = right->next;
            }
            return left;
        }
    }
    return NULL;
}